package com.gzy.cs3.service.impl;

import com.gzy.cs3.mapper.UserMapper;
import com.gzy.cs3.model.User;
import com.gzy.cs3.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        try {
            logger.info("Getting all users");
            return userMapper.getAllUsers();
        } catch (DataAccessException e) {
            logger.error("Failed to get all users", e);
            throw new RuntimeException("Failed to get all users", e);
        }
    }

    @Override
    public User getUserById(int id) {
        if (id <= 0) {
            throw new IllegalArgumentException("Invalid user ID: " + id);
        }
        try {
            logger.info("Getting user by ID: {}", id);
            User user = userMapper.getUserById(id);
            if (user == null) {
                logger.error("User not found with ID: {}", id);
                throw new RuntimeException("User not found with ID: " + id);
            }
            return user;
        } catch (DataAccessException e) {
            logger.error("Failed to get user by ID: {}", id, e);
            throw new RuntimeException("Failed to get user by ID: " + id, e);
        }
    }

    @Override
    public void insertUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User object cannot be null");
        }
        if (user.getName() == null || user.getName().isEmpty()) {
            throw new IllegalArgumentException("User name cannot be null or empty");
        }
        if (user.getAge() < 0) {
            throw new IllegalArgumentException("User age cannot be negative");
        }
        try {
            logger.info("Inserting user: {}", user);
            userMapper.insertUser(user);
        } catch (DataAccessException e) {
            logger.error("Failed to insert user: {}", user, e);
            throw new RuntimeException("Failed to insert user", e);
        }
    }

    @Override
    public void updateUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User object cannot be null");
        }
        if (user.getId() <= 0) {
            throw new IllegalArgumentException("Invalid user ID: " + user.getId());
        }
        if (user.getName() == null || user.getName().isEmpty()) {
            throw new IllegalArgumentException("User name cannot be null or empty");
        }
        if (user.getAge() < 0) {
            throw new IllegalArgumentException("User age cannot be negative");
        }
        try {
            logger.info("Updating user: {}", user);
            userMapper.updateUser(user);
        } catch (DataAccessException e) {
            logger.error("Failed to update user: {}", user, e);
            throw new RuntimeException("Failed to update user", e);
        }
    }

    @Override
    public void deleteUser(int id) {
        if (id <= 0) {
            throw new IllegalArgumentException("Invalid user ID: " + id);
        }
        try {
            logger.info("Deleting user with ID: {}", id);
            userMapper.deleteUser(id);
        } catch (DataAccessException e) {
            logger.error("Failed to delete user with ID: {}", id, e);
            throw new RuntimeException("Failed to delete user with ID: " + id, e);
        }
    }
}
